﻿
namespace TinyMceWrapper.Web.Configuration
{

    #region [ Imports ]

    using System;
    using System.ComponentModel;
    using System.Configuration;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;

    #endregion

    [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Mce")]
    public sealed class TinyMceOption : ConfigurationElement
    {

        #region [ Local Fields ]

        private static readonly ConfigurationProperty _key = new ConfigurationProperty("key", typeof(string), null, null, new StringValidator(1), ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _value = new ConfigurationProperty("value", typeof(object), null, new ValueTypeConverter(), null, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _urlOption = new ConfigurationProperty("urlOption", typeof(bool), false);
        private static readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

        #endregion

        #region [ Constructors ]

        [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
        static TinyMceOption()
        {
            _properties.Add(_key);
            _properties.Add(_value);
            _properties.Add(_urlOption);
        }

        #endregion

        #region [ Public Properties ]

        [ConfigurationProperty("key", IsKey = true, IsRequired = true)]
        public string Key
        {
            get { return (string)this[_key]; }
            set { this[_key] = value; }
        }

        [ConfigurationProperty("value", IsRequired = true)]
        public object Value
        {
            get { return this[_value]; }
            set { this[_value] = value; }
        }

        [ConfigurationProperty("urlOption")]
        public bool UrlOption
        {
            get { return (bool)this[_urlOption]; }
            set { this[_urlOption] = value; }
        }

        #endregion

        #region [ Overriden Members ]

        protected override ConfigurationPropertyCollection Properties
        {
            get { return _properties; }
        }

        #endregion

        private sealed class ValueTypeConverter : TypeConverter
        {

            #region [ Overriden Members ]

            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return (sourceType == typeof(string));
            }

            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                return (destinationType == typeof(string) || destinationType == typeof(bool) || destinationType == typeof(int));
            }

            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                string str = (string)value;
                double num;
                bool b;

                if (Utilities.IsNumeric(str, out num))
                    return num;
                else if (Utilities.IsBoolean(str, out b))
                    return b;

                return str;
            }

            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                return value.ToString();
            }

            #endregion

        }

    }

}
